//
//  ViewController.m
//  temp
//
//  Created by Jorn.Wu on 15/5/22.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *tfOne;
@property (weak, nonatomic) IBOutlet UITextField *tfTwo;
@property (weak, nonatomic) IBOutlet UILabel *result;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (nonatomic, assign) double num1;
@property (nonatomic, assign) double num2;

- (IBAction)calculate:(id)sender;
- (IBAction)subBtn:(id)sender;
- (IBAction)mulBtn:(id)sender;
- (IBAction)divBtn:(id)sender;
- (IBAction)extraBtn:(id)sender;
- (IBAction)backZero:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.tfOne.delegate = self;
    self.tfTwo.delegate = self;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.tfOne resignFirstResponder];
    [self.tfTwo resignFirstResponder];
}

- (void)calculator:(UniChar)myChar{
    self.num1 = [self.tfOne.text doubleValue];
    self.num2 = [self.tfTwo.text doubleValue];
    double caResult;
    switch (myChar) {
        case '+':
            caResult = self.num1 + self.num2;
            self.result.text = [NSString stringWithFormat:@"%lf",caResult];
            break;
        case '-':
            caResult = self.num1 - self.num2;
            self.result.text = [NSString stringWithFormat:@"%lf",caResult];
            break;
        case '*':
            caResult = self.num1 * self.num2;
            self.result.text = [NSString stringWithFormat:@"%lf",caResult];
            break;
        case '/':
            caResult = self.num1 / self.num2;
            self.result.text = [NSString stringWithFormat:@"%lf",caResult];
            break;
        default:
            self.result.text = [NSString stringWithFormat:@"error!"];
            break;
    }
}

- (IBAction)calculate:(id)sender {
    [self calculator:'+'];
    self.label.text = @"＋";
    [self.tfOne resignFirstResponder];
    [self.tfTwo resignFirstResponder];
}

- (IBAction)subBtn:(id)sender {
    [self calculator:'-'];
    self.label.text = @"－";
    [self.tfOne resignFirstResponder];
    [self.tfTwo resignFirstResponder];
}

- (IBAction)mulBtn:(id)sender {
    [self calculator:'*'];
    self.label.text = @"×";
    [self.tfOne resignFirstResponder];
    [self.tfTwo resignFirstResponder];
}

- (IBAction)divBtn:(id)sender {
    [self calculator:'/'];
    self.label.text = @"÷";
    [self.tfOne resignFirstResponder];
    [self.tfTwo resignFirstResponder];
}

- (IBAction)extraBtn:(id)sender {
    NSInteger numOne = [self.tfOne.text integerValue];
    NSInteger numTwo = [self.tfTwo.text integerValue];
    
    NSInteger caResult = numOne % numTwo;
    self.result.text = [NSString stringWithFormat:@"%li",(long)caResult];
    self.label.text = @"%";
    [self.tfOne resignFirstResponder];
    [self.tfTwo resignFirstResponder];
}

- (IBAction)backZero:(id)sender {
    self.tfOne.text = @"";
    self.tfTwo.text = @"";
    self.result.text = @"0";
}
@end
